package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.anno.Log;
import com.southwind.common.DataGridView;
import com.southwind.common.ResultObj;
import com.southwind.common.SalaryPageModel;
import com.southwind.entity.AdInfo;
import com.southwind.service.AdService;
import com.southwind.vo.AdInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/salaryRecord")
public class AdInfoController {

    @Autowired
    private AdService adService;

    @RequestMapping("/selectAllList")
    public DataGridView loadAllSalaryRecord(SalaryPageModel pageModel){
        Page<AdInfo> page = new Page<>(pageModel.getPage(), pageModel.getLimit());
        QueryWrapper<AdInfo> queryWrapper = new QueryWrapper<>();//构造查询条件
        queryWrapper.eq(StringUtils.isNotBlank(pageModel.getSalaryMonth()), "salary_month", pageModel.getSalaryMonth());
        queryWrapper.like(StringUtils.isNotBlank(pageModel.getAdName()), "ad_name", pageModel.getAdName());
        Page<AdInfo> resultPage = this.adService.page(page,queryWrapper);
        List<AdInfoVO> list = new ArrayList<>();
        for (AdInfo record : resultPage.getRecords()) {
            AdInfoVO vo = new AdInfoVO();//获取姓名、部门
            BeanUtils.copyProperties(record, vo);
//            vo.setName(this.salaryRecordMapper.getUserNameById(record.getId()));
//            vo.setDeptname(this.salaryRecordMapper.getDeptNameById(record.getId()));
            list.add(vo);
        }
        DataGridView dataGridView = new DataGridView(resultPage.getTotal(), list);
        return dataGridView;
    }

    @RequestMapping("/addSalaryRecord")
    public ResultObj addSalaryRecord(AdInfo adInfo){
        boolean save = this.adService.save(adInfo);
        if(save) return ResultObj.ADD_SUCCESS;
        return ResultObj.ADD_ERROR;
    }
    @Log
    @RequestMapping("/updateSalaryRecord")
    public ResultObj updateSalaryRecord(AdInfo adInfo){
        boolean updateById = this.adService.updateById(adInfo);
        if(updateById) return ResultObj.UPDATE_SUCCESS;
        return ResultObj.UPDATE_ERROR;
    }
    @Log
    @RequestMapping("/deleteSalaryRecord")
    public ResultObj deleteSalaryRecord(Integer id){
        boolean removeById = this.adService.removeById(id);
        if(removeById) return ResultObj.DELETE_SUCCESS;
        return ResultObj.DELETE_ERROR;
    }

    @RequestMapping("/batchDeleteSalaryRecord")
    public ResultObj batchDeleteSalaryRecord(Integer[] ids){
        List<Integer> idList = Arrays.asList(ids);
        boolean removeByIds = this.adService.removeByIds(idList);
        if(removeByIds) return ResultObj.DELETE_SUCCESS;
        return ResultObj.DELETE_ERROR;
    }

}

